package com.yc.baseProgram.jdk8;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestStream {
	
	private List<String> getDateList(String startDate, String endDate) {
		return collectLocalDates(LocalDate.parse(startDate), LocalDate.parse(endDate));
	}
	
	private List<String> collectLocalDates(LocalDate start, LocalDate end) {
		//以起始时间作为流的源头，按照每次加一天的方式创建一个无限流
		return Stream.iterate(start, localDate -> localDate.plusDays(1))
				.limit(ChronoUnit.DAYS.between(start, end) + 1)//截断无限流，长度为起始时间到结束时间差的+1
				.map(LocalDate::toString)//最后输出的是字符串，转字符串
				.collect(Collectors.toList());//把流收集为List
	}

	public static void main(String[] args) {
		TestStream ts = new TestStream();
		List<String> list = ts.getDateList("2020-07-27", "2020-08-08");
		for (String str : list) {
			System.out.println(str);
		}
	}

}
